package com.leonzx.base.controller.excel;

import com.leonzx.base.config.pager.Page;
import com.leonzx.base.config.pager.PageInfo;
import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.ExcelTemplate;
import com.leonzx.base.entity.system.Orders;
import com.leonzx.base.service.system.ExcelTemplateService;
import com.leonzx.base.service.system.OrdersService;
import com.leonzx.base.service.system.UserService;
import com.leonzx.base.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author:张勇
 * @Description:
 * @Date: 2018-06-11 9:27
 */
@RestController
@RequestMapping("/ajax/excelTemplate")
public class AjaxExcelTemplateController extends BaseController {
    @Autowired
    private ExcelTemplateService excelTemplateService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;

    /**
     * excel模板列表
     *
     * @param excelTemplate
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public Map<String, Object> list(ExcelTemplate excelTemplate, @PageInfo(pageSize = Page.PAGE_SIZE, orderBy = "create_time") Page<ExcelTemplate> page) {
        Map<String, Object> result = getMap();
        try {
            List<ExcelTemplate> baseExcelTemplatees = excelTemplateService.getAllBaseBjBjjj(excelTemplate);
            page.setResult(baseExcelTemplatees);
            SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            //循环list，处理数据
            for (ExcelTemplate e : baseExcelTemplatees) {
                String createTimeStr = "";
                if (e.getCreateTime() != null) {
                    createTimeStr = sdf.format(e.getCreateTime());
                }
                String createUserName = userService.getUserNameById(e.getCreateUserId());
                e.setCreateTimeStr(createTimeStr);
                e.setCreateUserName(createUserName);
            }
            result.put("count", page.getTotalCount());
            result.put("data", baseExcelTemplatees);
            result.put("code", "0");
            result.put("msg", "加载完成");
        } catch (Exception e) {
            logger.error("获取委托单数据失败", e);
            addFailMessage(result, "获取委托单数据失败");
        }
        return result;
    }
    /**
     * 添加excel模板或者更新信息
     *
     * @param excelTemplate
     * @return
     */
    @RequestMapping("/save")
    public Map<String, Object> saveorupdate(ExcelTemplate excelTemplate) {
        Map<String, Object> result = getMap();
        try {
            if(Objects.isNull(excelTemplate.getId())){
                excelTemplate.setCreateUserId(currentUser().getId());
                excelTemplateService.insert(excelTemplate);
                addSuccessMessage(result, "保存Excel模板成功");
            }else{
                excelTemplateService.update(excelTemplate);
                addSuccessMessage(result, "Excel模板更新成功");
            }
        } catch (Exception e) {
            logger.error("保存Excel模板失败", e);
            addFailMessage(result, "保存Excel模板失败");
        }
        return result;
    }

    @RequestMapping("/getOrders")
    public Map<String, Object> getOrders (String name, Orders orders, @PageInfo(pageSize = Page.PAGE_SIZE, orderBy = "create_time") Page<Orders> page){
        Map<String, Object> result = getMap();
        try{
            orders.setName(name);
            orders.setState(Constants.STATE_VAILD);
            List<Orders> ordersList = ordersService.getAllBaseOrders(orders);
            page.setResult(ordersList);
            result.put("count", page.getTotalCount());
            result.put("data", ordersList);
            result.put("code", "0");
            result.put("msg", "加载完成");
        }catch (Exception e){
            logger.error("获取委托单列表失败",e);
            addFailMessage(result, "获取委托单列表失败");
        }
        return result;
    }


}
